import WebIMConfig from '../../untils/im/webim.config';
console.log(WebIMConfig.config);
// WebIMConfig.config = {
//  /*
//   * XMPP server
//   */
//  xmppURL: 'im-api.easemob.com',
//  /*
//   * Backend REST API URL
//   */
//  // apiURL: `${(location.protocol === 'https:' ? 'https:' : 'http:')}//a1.easemob.com`,
//  apiURL: 'https://a1.easemob.com',
//  /*
//   * Application AppKey
//   */
//  appkey: '1165170306178329#com.etzcn.eztpalm',
//  /*
//   * Whether to use wss
//   * @parameter {Boolean} true or false
//   */
//  https: true,
//  /*
//   * isMultiLoginSessions
//   * true: A visitor can sign in to multiple webpages and receive messages at all the webpages.
//   * false: A visitor can sign in to only one webpage and receive messages at the webpage.
//   */
//  isMultiLoginSessions: true,
//  /*
//   * Set to auto sign-in
//   */
//  isAutoLogin: true,
//  /**
//   * Whether to use window.doQuery()
//   * @parameter {Boolean} true or false
//   */
//  isWindowSDK: true,
//  /**
//   * isSandBox=true:  xmppURL: 'im-api-sandbox.easemob.com',  apiURL: '//a1-sdb.easemob.com',
//   * isSandBox=false: xmppURL: 'im-api.easemob.com',          apiURL: '//a1.easemob.com',
//   * @parameter {Boolean} true or false
//   */
//  isSandBox: false,
//  /**
//   * Whether to console.log in strophe.log()
//   * @parameter {Boolean} true or false
//   */
//  isDebug: false,
//  /**
//   * will auto connect the xmpp server autoReconnectNumMax times
//   * in background when client is offline.
//   * won't auto connect if autoReconnectNumMax=0.
//   */
//  autoReconnectNumMax: 2,
//  /**
//   * the interval secons between each atuo reconnectting.
//   * works only if autoReconnectMaxNum >= 2.
//   */
//  autoReconnectInterval: 2,
//  /**
//   * webrtc supports WebKit and https only
//   */
//  isWebRTC: /WebKit/.test(navigator.userAgent) && /^http$/.test(window.location.protocol),
//  /**
//   * after login, send empty message to xmpp server like heartBeat every 45s,
//   * to keep the ws connection alive.
//   */
//  heartBeatWait: 4500,
//  /**
//   * while http access,use ip directly,instead of ServerName,avoiding DNS problem.
//   */
//  isHttpDNS: false
//};
// 为WebIM添加配置
const WebIM = require('easemob-websdk');
console.log("ka");
WebIM.config=WebIMConfig.config
// 为WebIM添加配置

//创建连接
const conn = new WebIM.connection({
  https: WebIMConfig.config.https,
  url: WebIMConfig.config.xmppURL,
  isAutoLogin: WebIMConfig.config.isAutoLogin,
  isMultiLoginSessions: WebIMConfig.config.isMultiLoginSessions,
  isDebug: WebIMConfig.config.isDebug
});
console.log("开始连接");
conn.listen({
  onOpened: function ( message ) {          //连接成功回调
    console.log("成功");
    // 如果isAutoLogin设置为false，那么必须手动设置上线，否则无法收消息
    // 手动上线指的是调用conn.setPresence(); 如果conn初始化时已将isAutoLogin设置为true
    // 则无需调用conn.setPresence();
  },
  onError: function ( message ) {
     console.log(message)

  }
});
function login({ username, password }) {
  const options = {
    apiUrl: WebIMConfig.config.apiURL,
    user: username,
    pwd: password,
    appKey: WebIMConfig.config.appkey,
    success(token) {
      const accessToken = token.access_token;
      console.log(accessToken);
      // WebIM.utils.setCookie('webim_test', accessToken, 1);
    },
    error(err) {
      console.log(err);
    },
  };
  conn.open(options);
}

// 单聊发送文本消息
function sendMessage({ username, message }) {
  const id = conn.getUniqueId();                 // 生成本地消息id
  const msg = new WebIM.message('txt', id);      // 创建文本消息
  msg.set({
    msg: message,                  // 消息内容
    to: username,                          // 接收消息对象（用户id）
    roomType: false,
    success(id, serverMsgId) {
      console.log('send private text Success');
      console.log(serverMsgId);
    },
    error(err) {
      console.log(err);
    },
  });
  msg.body.chatType = 'singleChat';
  conn.send(msg.body);
}

export default {
   conn,WebIM,
};
